// store/index.js
import { createStore } from 'vuex';

export default createStore({
  state: {
    user: {
      name: '',
      token: ''
    },
    count: 0
  },
  getters: {
    isLogin: (state) => !!state.user.token, // 派生“是否登录”状态
    doubleCount: (state) => state.count * 2
  },
  mutations: {
    SET_USER(state, user) {
      state.user = user; // 同步修改用户信息
    },
    INCREMENT(state) {
      state.count++; // 同步修改计数
    }
  },
  actions: {
    // 异步登录示例
    login({ commit }, userInfo) {
      return new Promise((resolve) => {
        // 模拟接口请求
        setTimeout(() => {
          commit('SET_USER', { 
            name: userInfo.name, 
            token: 'mock_token_123' 
          });
          resolve('登录成功');
        }, 1000);
      });
    }
  },
  modules: {
    // 可拆分模块（如 cart.js、order.js 等）
  }
});